package com.cwt_taxi_xa.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.cwt_taxi_xa.protocol.DispatchCenterUsers;
import com.cwt_taxi_xa.util.AppLog;

/**
 * Created by undi on 2018/6/20/020.
 */

public class ConnectivityReceiver extends BroadcastReceiver {
    String TAG = "ConnectivityReceiver";
    /**
     * The absence of a connection type.
     * @hide
     */
    public static final int TYPE_NONE        = -1;

    private long lastReTime = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent != null){
            String action = intent.getAction();
            AppLog.d(TAG,"action :" + action);
            if(ConnectivityManager.CONNECTIVITY_ACTION.equals(action)){
                //获取联网状态的NetworkInfo对象
                int networkType = intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE,TYPE_NONE/*ConnectivityManager.TYPE_NONE*/);
                //NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                if(networkType >=0){
                    ConnectivityManager mConnectivityManager= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
                    if(mConnectivityManager != null){
                        NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
                        if (info != null) {
                            //如果当前的网络连接成功并且网络连接可用
                            if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                                if (info.getType() == ConnectivityManager.TYPE_WIFI
                                        || info.getType() == ConnectivityManager.TYPE_MOBILE) {
                                    //startCommand(context);
                                    long  nanoTime = System.nanoTime();
                                    //连续来了几次有点虚
                                    AppLog.i(TAG, getConnectionType(info.getType()) + "连上 nanoTime:" + nanoTime );
                                    if((lastReTime - nanoTime) > 800){
                                        lastReTime = System.nanoTime();
                                        //DispatchCenterUsers.startTimerConnectSvr();
                                    }
									DispatchCenterUsers.socketManager.setConnect(true);
                                }
                            } else {
                                //startStop(context);
                                AppLog.i(TAG, getConnectionType(info.getType()) + "断开");
                                DispatchCenterUsers.socketManager.setConnect(false);
                            }
                        }else{
                            //startStop(context);
                            AppLog.i(TAG, "断开 NetworkInfo is null");
                            DispatchCenterUsers.socketManager.setConnect(false);
                        }
                    }else{
                        AppLog.i(TAG, "断开 mConnectivityManager is null");
                        DispatchCenterUsers.socketManager.setConnect(false);
                    }

                }else{
                    AppLog.i(TAG, "断开 networkType is TYPE_NONE");
                    DispatchCenterUsers.socketManager.setConnect(false);
                }

            }
        }else{
            AppLog.d(TAG,"intent is null !");
        }
    }

    private String getConnectionType(int type) {
        String connType = "";
        if (type == ConnectivityManager.TYPE_MOBILE) {
            connType = "3G网络数据";
        } else if (type == ConnectivityManager.TYPE_WIFI) {
            connType = "WIFI网络";
        }
        return connType;
    }
}
